package com.git.bds.mr.hospital.service.hospital;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.git.bds.mr.doctor.mapper.DoctorMapper;
import com.git.bds.mr.doctor.model.domain.Doctor;
import com.git.bds.mr.doctor.model.dto.DoctorInfoDTO;
import com.git.bds.mr.hospital.convert.DoctorConvert;
import com.git.bds.mr.hospital.mapper.hospital.HospitalMapper;
import com.git.bds.mr.hospital.model.domain.Hospital;
import com.git.bds.mr.hospital.model.domain.Sector;
import com.git.bds.mr.hospital.model.vo.HospitalDoctorVO;
import com.git.bds.mr.page.PageParam;
import com.git.bds.mr.page.PageResult;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 医院信息表 服务实现类
 * </p>
 *
 * @author 成大事
 * @since 2022-09-19 16:42:52
 */
@Service
public class HospitalServiceImpl extends MPJBaseServiceImpl<HospitalMapper, Hospital> implements HospitalService {

    @Resource
    private DoctorMapper doctorMapper;

    /**
     * 添加医生
     *
     * @param doctorInfoDTO 医生信息dto
     * @return {@link Boolean}
     */
    @Override
    public Boolean addDoctor(DoctorInfoDTO doctorInfoDTO) {
        Doctor doctor = DoctorConvert.INSTANCE.toDoctor(doctorInfoDTO);
        return doctorMapper.insert(doctor) > 0;
    }

    /**
     * 修改医生
     *
     * @param doctorInfoDTO 医生信息dto
     * @return {@link Boolean}
     */
    @Override
    public Boolean modifyDoctor(DoctorInfoDTO doctorInfoDTO) {
        Doctor doctor = DoctorConvert.INSTANCE.toDoctor(doctorInfoDTO);
        return doctorMapper.updateById(doctor) > 0;
    }

    /**
     * 请所有医生
     *
     * @return {@link PageResult}<{@link HospitalDoctorVO}>
     */
    @Override
    public PageResult<HospitalDoctorVO> getAllDoctors(PageParam pageParam) {
        IPage<HospitalDoctorVO> allDoctors = doctorMapper.selectJoinPage(new Page<>(pageParam.getPageNo(), pageParam.getPageSize()), HospitalDoctorVO.class, new MPJLambdaWrapper<HospitalDoctorVO>()
                .select(Doctor::getName, Doctor::getId, Doctor::getAvatar, Doctor::getIndicationDirection)
                .leftJoin(Sector.class, Sector::getId, Doctor::getSectorId)
                .leftJoin(Hospital.class, Hospital::getId, Sector::getHospitalId)
                .eq(Hospital::getId, StpUtil.getLoginIdAsLong()));
        return new PageResult<>(allDoctors.getRecords(),allDoctors.getSize());
    }
}
